package com.github.base.service.impl;

import com.github.base.domain.UserRole;
import com.github.base.mapper.UserRoleMapper;
import com.github.base.service.UserRoleService;
import com.github.base.vo.user.UserRoleVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description: 账号
 * @author: dongdong.wen
 * @date: 2021/1/28 8:12 下午
 **/
@Slf4j
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;


    /**
     * 批量添加用户角色
     *
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAllBatch(List<UserRole> list) {
        boolean b = saveBatch(list);
        return b;
    }

    /**
     * 根据userId查询角色dto列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserRole> getByUserId(Long userId) {
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper().lambda();
        lambda.eq(UserRole::getUserId, userId);
        List<UserRole> list = userRoleMapper.selectList(lambda);
        return list;
    }

    /**
     * 根据userId删除角色信息
     *
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long userId) {
        LambdaQueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>().lambda();
        wrapper.eq(UserRole::getUserId, userId);
        userRoleMapper.delete(wrapper);
    }

    /**
     * 查询账号对应角色
     *
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserRoleVO> getUserRoleVoByUserId(Long userId) {
        return userRoleMapper.getUserRoleVoByUserId(userId);
    }
}
